<?php

namespace App\Http\Controllers\Admin;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\ShishengRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class ShishengController extends Controller
{

     /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
   
       
    public function getIndex(Request $request)
    {
        //每页显示条数
        $num = $request->input('num',2);

        //检测是否提交查询关键字
        if($request->has('keywords')){
             //分页查询 select * from user where username like '%admin%';
            $cates = DB::table('shishen1')->where('username','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            $cates = DB::table('shishen1')->paginate($num);
        }

        //提取参数 分配到页面中
        $data = $request->except('page');


        //解析模板 分配数据
        return view('Admin.shisheng.index',['users'=>$cates,'data'=>$data]);
    }
   



    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
       return view('Admin.shisheng.add');
    }
	
	 /**
     *后台式神添加处理
     *
     *@return insert 式神添加数据
     */
	 
	   public function postInsert(Request $request){
     
           //除了某个不要
        $data=$request->except('_token');

         $this->validate($request,[
            'username'=>'required|max:255',
            'img'=>'required',
            'pic'=>'required',
            'grade'=>'required',
            'miaoshu'=>'required',
            'gongji'=>'required',
            'fangyu'=>'required',
            'sudu'=>'required',
            'baoji'=>'required',
            'shanghai'=>'required',
            'mingzhong'=>'required',
            'dikang'=>'required',
            'jineng1'=>'required',
            'jineng11'=>'required',
            'jineng2'=>'required',
            'jineng22'=>'required',
            'jineng3'=>'required',
            'jineng33'=>'required',
          
            ],[
                'username.required'=>'标题必须填写',
                'img.required'=>'请上传图片(觉醒前)',
                'pic.required'=>'请上传图片(觉醒后)',
                'grade.required'=>'请选择等级',
                'miaoshu.required'=>'请填写描述',
                'gongji.required'=>'请设置攻击值',
                'fangyu.required'=>'请设置防御值',
                'sudu.required'=>'请设置速度值',
                'baoji.required'=>'请设置暴击值',
                'shanghai.required'=>'请设置暴击伤害值',
                'mingzhong.required'=>'请设置效果命中值',
                'dikang.required'=>'请设置效果抵抗值',
                'jineng1.required'=>'请上传技能图片(1)',
                'jineng11.required'=>'请上传技能名称(1)',
                'jineng2.required'=>'请上传技能图片(2)',
                'jineng22.required'=>'请上传技能名称(2)',
                'jineng3.required'=>'请上传技能图片(3)',
                'jineng33.required'=>'请上传技能名称(3)',

                
               
            ]);


         //检测是否有文件上传
        $data['img']=$this->upload1($request);
        $data['pic']=$this->upload($request);
        $data['jineng1']=$this->upload2($request);
       $data['jineng2']=$this->upload3($request);
        $data['jineng3']=$this->upload4($request);
      //dd($data);

      //把数据插到数据库
        $res=DB::table('shishen1')->insertGetId($data);
       // dd($res);
        if($res){

            return redirect('admin/shisheng/index')->with('success','式神添加成功');
        }else{
            return back()->with('error','式神添加失败');
        }
 }


         //文件上传操作
         public function upload($request){
           
            //检测是否有文件上传(觉醒前)
            if($request->hasFile('pic')){
                  //随机名
                $name=md5(time()+rand(1,999));
                  //后缀名
             $su=$request->file('pic')->getClientOriginalExtension();
               $request->file('pic')->move('./public/uploads/shisheng',$name.'.'.$su);
                return './public/uploads/shisheng/'.$name.'.'.$su;
        
        };
    }

       public function upload1($request){
           
            //检测是否有文件上传(觉醒后)
            if($request->hasFile('img')){
                  //随机名
                $name=md5(time()+rand(1,999));
                  //后缀名
             $su=$request->file('img')->getClientOriginalExtension();
               $request->file('img')->move('./public/uploads/shisheng',$name.'.'.$su);
                return './public/uploads/shisheng/'.$name.'.'.$su;
        
        };
    }

     public function upload2($request){
           
            //检测是否有文件上传(技能1)
            if($request->hasFile('jineng1')){
                  //随机名
                $name=md5(time()+rand(1,999));
                  //后缀名
             $su=$request->file('jineng1')->getClientOriginalExtension();
               $request->file('jineng1')->move('./public/uploads/jineng',$name.'.'.$su);
                return './public/uploads/jineng/'.$name.'.'.$su;
        
        };
    }
    
   public function upload3($request){
           
            //检测是否有文件上传(技能)
            if($request->hasFile('jineng2')){
                  //随机名
                $name=md5(time()+rand(1,999));
                  //后缀名
             $su=$request->file('jineng2')->getClientOriginalExtension();
               $request->file('jineng2')->move('./public/uploads/jineng',$name.'.'.$su);
                return './public/uploads/jineng/'.$name.'.'.$su;
        
        };
    }

     public function upload4($request){
           
            //检测是否有文件上传(技能)
            if($request->hasFile('jineng3')){
                  //随机名
                $name=md5(time()+rand(1,999));
                  //后缀名
             $su=$request->file('jineng3')->getClientOriginalExtension();
               $request->file('jineng3')->move('./public/uploads/jineng',$name.'.'.$su);
                return './public/uploads/jineng/'.$name.'.'.$su;
        
        };
    }

    //式神删除
    public function getDel(Request $request){

        //获取
       $id=$request->input('id');
       //dd($id);
       //执行
       $res=DB::table('shishen1')->where('id',$id)->delete();

        echo $res;

    }

     //用户修改
    public function getEdit($id){

       //根据id查信息
        $users=DB::table('shishen1')->where('id',$id)->first();
    

  
  


        //解析模板
       return view('Admin.shisheng.edit',['users'=>$users]);
    }


    //执行用户数据的修改
   public function postUpdate(Request $request){

        $data=$request->except('_token');
      // dd($data);


         if($request->hasFile('pic')){
            //调用方法进行觉醒头像上传
         $data['pic']=$this->upload($request);
         //dd($data);
      }
        
       
       // dd($data);
       //执行数据修改
        $res=DB::table('shishen1')->where('id',$data['id'])->update($data);
       //dd($res);

       if($res){

             //重定向到user/index
            return redirect('admin/shisheng/index')->with('success','式神修改成功');
        }else{
            //回到上个页面
            return back()->with('error','式神修改失败');

        }

        
    }


    
}
